﻿using System;
using System.Windows;

using PasswordProvider.ViewModel.Properties;
using PasswordProvider.ObjectModel;
using PasswordProvider.ViewModel.Framework;
using PasswordProvider.ViewModel.Helpers;

namespace PasswordProvider.ViewModel
{
    /// <summary>
    /// View model for the details view of a login site.
    /// </summary>
    public class LogOnSiteDetailsViewModel : SiteDetailsViewModel
    {
        internal LogOnSiteDetailsViewModel(Site site, bool isNew)
            : base(site, isNew)
        {

        }

        #region Public

        /// <summary>
        /// Header for the Identity tab
        /// </summary>
        public static string IdentityGroupDisplayName
        {
            get { return Strings.LogOnSiteDetails_IdentityGroupDisplayName; }
        }
        /// <summary>
        /// Header for the LogOn tab
        /// </summary>
        public static string LogOnGroupDisplayName
        {
            get { return Strings.LogOnSiteDetails_LogOnGroupDisplayName; }
        }

        /// <summary>
        /// The site's Url
        /// </summary>
        public TextEditViewModel Url
        {
            get
            {
                if (_url == null)
                {
                    LogOnSite logonSite = Site as LogOnSite;
                    _url = new TextEditViewModel(
                        Strings.SiteDetails_Url_DisplayName,
                        Strings.SiteDetails_Url_ToolTip,
                        logonSite.Url != null ? logonSite.Url.AbsoluteUri : null);
                    _url.PropertyChanged += delegate { OnPropertyChanged("Url"); };
                }
                return _url;
            }
        } private TextEditViewModel _url;

        /// <summary>
        /// The icon url.
        /// </summary>
        public Uri IconUrl
        {
            get
            {
                LogOnSite logonSite = Site as LogOnSite;
                return FaveIconUrlHelper.GetFaveIconUri(logonSite.Url, logonSite.IconUrl);
            }
        }

        /// <summary>
        /// The login site's login id.
        /// </summary>
        public TextEditViewModel LogOnId
        {
            get 
            {
                if (_logOnId == null)
                {
                    _logOnId = new TextEditViewModel(
                        Strings.LogOnSiteDetails_LogOnId_DisplayName,
                        Strings.LogOnSiteDetails_LogOnId_ToolTip,
                        (Site as LogOnSite).LogOnId);
                    _logOnId.TextValueChanged += 
                        delegate 
                        { 
                            OnPropertyChanged("LogOnId"); 
                        };
                }
                return _logOnId; 
            }
        } private TextEditViewModel _logOnId;

        /// <summary>
        /// The login site password
        /// </summary>
        public EncryptedValueViewModel Password
        {
            get
            {
                if (_password == null)
                {
                    _password = new EncryptedValueViewModel(
                        Strings.LogOnSiteDetails_Password_DisplayName,
                        Strings.LogOnSiteDetails_Password_ToolTip, 16,
                        (Site as LogOnSite).EncryptedPassword);                    
                    _password.PasswordChanged += delegate { OnPropertyChanged("Password"); };
                }
                return _password;
            }
        } private EncryptedValueViewModel _password;

        /// <summary>
        /// The Show Clear Text Check box
        /// </summary>
        public ToggleViewModel ShowClearText
        {
            get
            {
                if (_showClearText == null)
                {
                    _showClearText = new ToggleViewModel(
                        Strings.LogOnSiteDetails_ShowClearText_DisplayString,
                        Strings.LogOnSiteDetails_ShowClearText_ToolTip,
                        Password.ShowClearText);
                    _showClearText.IsCheckedChanged += delegate
                    {
                        SetShowClearTextDependencies();
                    };
                }
                return _showClearText;
            }
        } private ToggleViewModel _showClearText;

        /// <summary>
        /// The ShowPasswordGenerator check box.
        /// </summary>
        public ToggleViewModel ShowPasswordGenerator
        {
            get
            {
                if (_showPasswordGenerator == null)
                {
                    _showPasswordGenerator = new ToggleViewModel(
                        Strings.LogOnSiteDetails_ShowPasswordGenerator_DisplayString,
                        Strings.LogOnSiteDetails_ShowPasswordGenerator_ToolTip,
                        ShowClearText.IsChecked, ShowClearText.IsChecked);
                    _showPasswordGenerator.IsCheckedChanged += delegate
                    {
                        if (_showPasswordGenerator.IsChecked)
                            PasswordGenerator.Visibility = System.Windows.Visibility.Visible;
                        else
                            PasswordGenerator.Visibility = System.Windows.Visibility.Hidden;
                    };
                }
                return _showPasswordGenerator;
            }
        } private ToggleViewModel _showPasswordGenerator;

        /// <summary>
        /// PasswordGenerator
        /// </summary>
        public TextEditViewModel PasswordGenerator
        {
            get
            {
                if (_passwordGenerator == null)
                {
                    _passwordGenerator = new TextEditViewModel(null, null, null);
                    _passwordGenerator.Visibility = ShowPasswordGenerator.IsChecked ? Visibility.Visible : Visibility.Hidden;
                    _passwordGenerator.TextValueChanged += delegate
                    {
                        Password.DisplayString = _passwordGenerator.Text;
                    };
                }
                return _passwordGenerator;
            }
        } private TextEditViewModel _passwordGenerator;

        #endregion // Public Interface

        #region Overrides

        /// <summary>
        /// Transfers the properties from this view model to the underlying LogOnSite.
        /// </summary>
        protected override void TransferProperties()
        {
            LogOnSite site = (LogOnSite)Site;
            site.LogOnId = LogOnId.Text;
            site.Url = UriHelper.CreateUri(Url.Text);            
            Password.CommitNewValue();            
        }
        #endregion // Overrides

        #region Private Methods

        /// <summary>
        /// Method to set all the properties that have dependency on ShowClearText.
        /// </summary>
        private void SetShowClearTextDependencies()
        {
            if (ShowClearText.IsChecked)
            {
                ShowPasswordGenerator.IsEnabled = Password.ShowClearText = true;
                PasswordGenerator.Visibility = ShowPasswordGenerator.IsChecked ? Visibility.Visible : Visibility.Hidden;
            }
            else
            {
                ShowPasswordGenerator.IsEnabled = Password.ShowClearText = false;
                PasswordGenerator.Visibility = Visibility.Hidden;
            }
        }

        #endregion

    }
}
